package com.zzx.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.boot.common.R;
import com.zzx.boot.mapper.EmployeeMapper;
import com.zzx.boot.pojo.Employee;
import com.zzx.boot.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

import static org.apache.commons.lang.StringUtils.isNotEmpty;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Resource
    private EmployeeMapper mapper;

    @Override
    public R<Employee> login(Employee employee) {
//        将数据加密 MD5
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes(StandardCharsets.UTF_8)));
        Employee user = mapper.selectByIdAndName(employee);
        /*判断是否查询到用户*/
        if (user!=null){
//            员工没被禁用
            if (user.getStatus()==1){
                return  R.success(user);
            }else {
                return R.error("员工已被禁用");
            }
        }else {
          return R.error("用户名或者密码错误");
        }

    }

    /**
     * 添加用户
     * @param employee
     * @return
     */
   //localhost:8080/backend/plugins/axios/axios.min.map: HTTP error: status code 404,
    @Override
    public R<String> addUser(Employee employee) {
        System.out.println(employee);
//        将密码转换成mo5格式
        String password = DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8));
        employee.setPassword(password);
        //设置默认密码
//        employee.setPassword("123456");
//        设置创建时间
//        employee.setCreateTime(LocalDateTime.now());
//        设置修改时间
//        employee.setUpdateTime(LocalDateTime.now());
//        Long userid = Long.parseLong((String) session.getAttribute("employee"));
//        设置员工添加人
//        employee.setCreateUser(userid);
//        设置员工修改人
//        employee.setUpdateUser(userid);
            int insert = mapper.insert(employee);
            if (insert == 1) {
                return R.success("用户添加成功");
            } else {
                return R.error("用户添加失败");
            }

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page> pageSelect(Integer page,Integer pageSize,String name){

        //创建分页类
        Page<Employee> page1 = new Page(page,pageSize);
        //创建一个LambdaQuery的条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //当name存在的时候  就 执行该语句  第一个参数 判断是否为null, 第二个参数 是表中的哪个字段名称(模糊查询的字段)  字段名称的值(条件的值)
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        Page<Employee> employeePage = mapper.selectPage(page1, queryWrapper);
        return R.success(employeePage);
    }

    @Override
    public R<String> IsStatus(Employee employee) {
        //更新时间
//        employee.setUpdateTime(LocalDateTime.now());
        //更新人
//        String empId = (String) session.getAttribute("employee");
//        employee.setUpdateUser(Long.parseLong(empId));
        int i =-1;
        try{
//            i = mapper.UpdateBYId(employee);
            i =mapper.updateById(employee);
            if (i==-1){
                i/=0;
            }
        }catch (Exception e){
            return R.error("修改失败!");
        }
        return R.success("修改成功!");

    }


}
